#ifndef CVSTEREO_H
#define CVSTEREO_H

#include <QObject>
#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;
class CVStereo : public QObject
{
    Q_OBJECT
public://function
    explicit CVStereo(QObject *parent = nullptr);
    void writeCamCalibrate();
    void readCamCalibrate();
    void writeStereoCalibrate();
    void readStereoCalibrate();
    void findCornersPoints(vector<Mat> imgs, vector<vector<Point2f>> &points, Size boardSize);
    void stereoCalibCalculate(vector<Mat> imgs_left, vector<Mat> imgs_right, Size boardSize);
    void stereoRectifyCalculate(vector<Mat> imgs_left, vector<Mat> imgs_right);
    void stereo_BM_match(Mat& xyz);
    void stereo_SGBM_match(Mat& xyz);

    cv::Mat rectify_undistort(cv::Mat& src);
    cv::Mat rectify_remap(cv::Mat& src);

    Point2f findObjectLeft(cv::Mat& img);
    Point2f findObjectRight(cv::Mat& img);
    Point3f uv2xyz(Point2f uvLeft, Point2f uvRight);
private://parameter
    Size imageSize;//!图像尺寸
    vector<vector<Point2f>> imgLsPoints;//!校正前，左眼图像的所有内角点坐标
    vector<vector<Point2f>> imgRsPoints;//!校正前，右眼图像的所有内角点坐标
    Mat Matrix_left, dist_left, Matrix_right, dist_right, rvecs, tvecs;//！左右相机内参Matrix与畸变系数dist；rvecs与tvecs无用
    Mat R, T, E, F;  //!双目标定旋转矩阵、平移向量、本征矩阵、基本矩阵
    Mat R1, R2, P1, P2, Q;//!校正变换矩阵
    Mat map_lx, map_ly, map_rx, map_ry;//!校正映射矩阵
    Rect validROIL;//!图像校正之后，会对图像进行裁剪，这里的validROI就是用于裁剪的矩形区域
    Rect validROIR;
    Mat left_rectify, right_rectify;//!校正后的图像
    //BM/SGBM测距算法参数
    int blockSize = 5, uniquenessRatio = 10, numDisparities = 7;

    //uv2xyz
    //左相机旋转矩阵
    float leftRotation[3][3] = {1,0,0,  0,1,0,  0,0,1 };
    //左相机平移向量
    float leftTranslation[1][3] = {0,0,0};
};

#endif // CVSTEREO_H
